home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / GrowZone.c < prev    next >
Encoding:
Text File  |  1997-05-24  |  4.4 KB  |  177 lines  |  [TEXT/CWIE]

  1. // stolen from PowerPlant, adapted to C
  2.  
  3. #include    "UtilsSys7.h"
  4. #include    "SimResIDs.h"
  5.  
  6. #include    "GrowZone.h"
  7. #include    "AEHandlers.h"
  8. #include    "SimUtils.h"
  9.  
  10. #define    kCushionSize    30720UL
  11. #define    kReserveSize    40960UL
  12.  
  13. static GrowZoneUPP    gMyGrowZone_UPP = nil;
  14. static Handle        gLocalReserve = nil;
  15. static Boolean        gGiveWarning = false;            
  16.  
  17.  
  18. static Boolean MemoryIsLow(void);
  19. static UInt32 DoGrowZone(UInt32 inBytesNeeded);
  20. static UInt32 UseLocalReserve(void);
  21. static pascal long theRealGZ(Size inBytesNeeded);
  22.  
  23. #pragma segment Init
  24. // ---------------------------------------------------------------------------
  25. //        • LGrowZone
  26. // ---------------------------------------------------------------------------
  27. //    Constructor
  28. //
  29. //    Allocates a memory reserve of the specified size and installs
  30. //    a GrowZone function
  31. //
  32. //    Installs the LGrowZone object in the Periodical Repeater queue so
  33. //    that its SpendTime() function will get called each time through the
  34. //    main event loop.
  35.  
  36. void InitGrowZone(void)
  37. {
  38. gLocalReserve = NewHandle(kReserveSize);
  39. gMyGrowZone_UPP = NewGrowZoneProc(theRealGZ);
  40. SetGrowZone(gMyGrowZone_UPP);
  41. }
  42.  
  43. #pragma segment CleanUp
  44.  
  45. // ---------------------------------------------------------------------------
  46. //        • ~LGrowZone
  47. // ---------------------------------------------------------------------------
  48. //    Destructor
  49. //
  50. //    Deallocates memory reserve and de-installs GrowZone function
  51.  
  52. void CleanupGrowZone(void)
  53. {
  54. if (gLocalReserve) {
  55.     DisposeHandle(gLocalReserve);
  56.     gLocalReserve = nil;
  57.     }
  58.  
  59. SetGrowZone(nil);
  60. DisposeRoutineDescriptor(gMyGrowZone_UPP);
  61. gMyGrowZone_UPP = nil;
  62. }
  63.  
  64. #pragma segment Main
  65. // ---------------------------------------------------------------------------
  66. //        • SpendTime
  67. // ---------------------------------------------------------------------------
  68. //    Try to reallocate our memory reserve if necessary and warn user if
  69. //    memory is getting low
  70. //
  71. //    Called each time through the main event loop
  72.  
  73. void GrowZone_Idle(void)
  74. {
  75. if (MemoryIsLow()) {
  76.     if (MaxBlock() > kReserveSize + 2048) {
  77.         if (gLocalReserve == nil)
  78.             gLocalReserve = NewHandle(kReserveSize);
  79.         else
  80.             ReallocateHandle(gLocalReserve, kReserveSize);
  81.         
  82.         if (!MemError())
  83.             gGiveWarning = false;
  84.         }
  85.     }
  86.  
  87. if (gGiveWarning) {
  88.     gGiveWarning = false;
  89.     if (Get1Resource('ALRT', kALRT_GZOUTOFMEM))
  90.         (void) CautionAlert_AE(kALRT_GZOUTOFMEM, myStdFilterProcNoCancel, myIdleFunct);
  91.     }
  92. }
  93.  
  94. // ---------------------------------------------------------------------------
  95. //        • MemoryIsLow
  96. // ---------------------------------------------------------------------------
  97. //    Return whether our memory reserve has been used
  98. //
  99. //    Clients can call this routine if they wish to behave differently in
  100. //    low memory situations. For example, a program could disable the "New"
  101. //    and "Open" commands to prevent new Documents from being created when
  102. //    memory is low.
  103.  
  104. Boolean MemoryIsLow(void)
  105. {
  106. return (gLocalReserve == nil || *gLocalReserve == nil);
  107. }
  108.  
  109. Boolean IsMemoryAvailable(UInt32 needed)
  110. {
  111. if (MemoryIsLow())
  112.     return false;
  113.  
  114. if (needed + kCushionSize < MaxBlock())
  115.     return true;
  116. else {
  117.     return needed + kCushionSize < MaxBlock();
  118.     }
  119. }
  120.  
  121.  
  122. // ---------------------------------------------------------------------------
  123. //        • DoGrowZone
  124. // ---------------------------------------------------------------------------
  125. //    Called by our Toolbox GrowZone function when the system needs more
  126. //    memory
  127.  
  128. UInt32 DoGrowZone(UInt32 /*inBytesNeeded*/)
  129. {
  130.  
  131. return UseLocalReserve();
  132. }
  133.  
  134. // ---------------------------------------------------------------------------
  135. //        • UseLocalReserve
  136. // ---------------------------------------------------------------------------
  137. //    Empty our local memory reserve in order to free up some memory
  138.  
  139. UInt32 UseLocalReserve(void)
  140. {
  141. UInt32    bytesFreed = 0;
  142.  
  143. if ( gLocalReserve &&
  144.      *gLocalReserve &&
  145.      gLocalReserve != GZSaveHnd() ) {
  146.      
  147.     EmptyHandle(gLocalReserve);
  148.     bytesFreed = kReserveSize;
  149.     gGiveWarning = true;
  150.     }
  151.  
  152. return bytesFreed;
  153. }
  154.  
  155.  
  156. // ---------------------------------------------------------------------------
  157. //        • GrowZoneCallBack [static]
  158. // ---------------------------------------------------------------------------
  159. //    This is the "real" GrowZone function registered with the System. It
  160. //    sets up the A5 world (68K) so we can access globals, then calls a
  161. //    virtual function for the LGrowZone class.
  162.  
  163. pascal long theRealGZ(Size inBytesNeeded)
  164. {
  165. #if !GENERATINGCFM
  166. long    theA5 = SetCurrentA5();
  167. #endif
  168. UInt32    bytesFreed = DoGrowZone(inBytesNeeded);
  169.  
  170. #if !GENERATINGCFM
  171. (void) SetA5(theA5);
  172. #endif
  173.  
  174. return bytesFreed;
  175. }
  176.  
  177.